package com.iot.inter.controller.newinterface.other;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.iot.inter.entity.common.DeviceFlowUsage;
import com.iot.inter.entity.newpojo.Constant;
import com.iot.inter.entity.newrequest.notify.request.RequestNotifyNew;
import com.iot.inter.entity.newrequest.notify.response.ResponseNotifyNew;
import com.iot.inter.interfaceprocessing.newinterface.TrunToNoticyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.*;

/**
 * 去北京做：3.15支付结果通知:2.0接口
 *
 * @author lm
 * @date 2019/3/4
 */
@RestController
@Api(description = "3.15支付结果通知")
public class NoticePaymentResultNewController {

    private static Logger logger = Logger.getLogger(NoticePaymentResultNewController.class);

    @CrossOrigin
    @RequestMapping(value = "notifyNew", produces = "application/json;charset=utf-8", method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "3.15支付结果通知", response = ResponseNotifyNew.class)
    public String notify(@ApiParam @RequestBody RequestNotifyNew request) {
        //3,定义要回复的内容
        String jsonString;
        String requestStr = JSON.toJSONString(request);
        if (requestStr != null) {
            logger.info("【支付结果通知】请求：" + requestStr);
            RequestNotifyNew requestPackage = JSONArray.parseObject(requestStr, RequestNotifyNew.class);
            String orderId = requestPackage.getOrderId();
            String deviceId = requestPackage.getDeviceId();
            String payType = requestPackage.getPayType();
            String payAmount = requestPackage.getPayAmount();
            String secret = requestPackage.getSecret();
            String partnerCode = requestPackage.getPartnerCode();

            TrunToNoticyService noticyService = new TrunToNoticyService();
            DeviceFlowUsage result = noticyService.notify(orderId, deviceId, payType, payAmount, secret, partnerCode);

            if (result != null) {
                if (true == Constant.RET_CODE_0000.equalsIgnoreCase(result.getRetCode())) {
                    ResponseNotifyNew responseOrder = new ResponseNotifyNew();
                    responseOrder.setRetCode(result.getRetCode());
                    responseOrder.setResultInfo(result.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【支付结果通知】成功响应：" + jsonString);
                    return jsonString;

                } else {
                    ResponseNotifyNew responseOrder = new ResponseNotifyNew();
                    responseOrder.setRetCode(result.getRetCode());
                    responseOrder.setResultInfo(result.getResultInfo());
                    // 返回给调用者的报文
                    jsonString = JSON.toJSONString(responseOrder);
                    logger.info("【支付结果通知】响应：" + jsonString);
                    return jsonString;
                }

            } else {
                ResponseNotifyNew responseOrder = new ResponseNotifyNew();
                responseOrder.setRetCode("9998");
                responseOrder.setResultInfo("响应报文为空");
                // 返回给调用者的报文
                jsonString = JSON.toJSONString(responseOrder);
                logger.info("【支付结果通知】响应：" + jsonString);
                return jsonString;

            }

        } else {
            ResponseNotifyNew responseOrder = new ResponseNotifyNew();
            responseOrder.setRetCode("9999");
            responseOrder.setResultInfo("请求报文为空");
            // 返回给调用者的报文
            jsonString = JSON.toJSONString(responseOrder);
            logger.info("【支付结果通知】响应：" + jsonString);
            return jsonString;

        }

    }

}
